package com.wk1un2.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wk1un2.entity.ProductCategory;
import com.wk1un2.service.ProductCategoryService;
import com.wk1un2.util.Result;

/**
 * 商品分类控制器
 * 
 * @author Administrator
 *
 */
@Controller
public class ProductCategoryController {
	@Autowired
	ProductCategoryService service;

	@RequestMapping("/Backstage/getAllProductCategory")
	@ResponseBody
	public Object getAll(@RequestParam("page") int page, @RequestParam("limit") int limit,
			@RequestParam("pcat_name") String pcat_name) {

		if (pcat_name == null || pcat_name == "") {
			pcat_name = null;
		}

		List<Map<String, Object>> ls = service.getAll((page - 1) * limit, limit, pcat_name);
		Result res = new Result(0, "", service.count(), ls);
		return res;
	}

//	
	@RequestMapping("/Backstage/addProductCategory")
	@ResponseBody
	public Object add(@RequestParam Map<String, Object> map) {

		System.out.println(map.get("cla_name"));
		System.out.println(map.get("cla_superior"));
		int i = service.add(map);
		Result res = new Result(0, "", i, i);
		return res;
	}

//	޸
	@RequestMapping("/Backstage/updateProductCategory")
	@ResponseBody
	public Object update(@RequestParam Map<String, Object> map) {
		System.out.println(map.get("cla_name"));
		System.out.println(map.get("cla_superior"));
		int i = service.update(map);
		Result res = new Result(0, "", i, i);
		return res;
	}

//	ɾ
	@RequestMapping("/Backstage/deleteProductCategory")
	@ResponseBody
	public Object delete(@RequestParam("pcat_id") Integer pcat_id) {
		System.out.println(pcat_id);
		int j = service.getProCount(pcat_id);
		System.out.println("#################################");
		System.out.println(j);
		if (j > 0) {
			Result res = new Result(0, "", 0, true);
			return res;
		} else {
			int i = service.delete(pcat_id);
			Result res = new Result(0, "", i, true);
			return res;
		}
	}
}